package lingnan.cn.edu.view;

import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;



import lingnan.cn.edu.Table.OrderTableModel;
import lingnan.cn.edu.User.Orderinf;
import lingnan.cn.edu.other.Other1;
import lingnan.cn.edu.service.OrderAndInfService;
import lingnan.cn.edu.util.FontUtil;
import lingnan.cn.edu.util.ShoppingCar;

public class OrderView extends Base {

	private JSplitPane containerPanel;
	private JPanel upPanel, downPanel;
	private JTable orderTable;
	private JPanel bottomPanel;
	private JLabel countInfoLabel,textPanel;
	private JButton commitButton,clearButton;
	private JScrollPane scrollpane = null;
	
	public static int id;
	public static int tp;
	
	
	public OrderView() {
		super(370, 500, "订单界面");
	}

	@Override
	protected void initView()
	
	{
		
		ShoppingCar shoppingCar = MainView.getShoppingCar();
		
        upPanel =new JPanel();
        upPanel.setBackground(Color.BLACK);
        upPanel.setLayout(null);
        upPanel.setSize(300, 400);
        
        downPanel =new JPanel();
        downPanel.setBackground(Color.red);
        downPanel.setLayout(null);
        downPanel.setSize(300, 400);
       
       
        
        containerPanel = new JSplitPane(JSplitPane.VERTICAL_SPLIT,upPanel,downPanel);
		
		Container container = getContentPane();
		container.add(containerPanel,"Center");
		
		//表格
		OrderTableModel orderTableModel = new OrderTableModel();
		orderTable = new JTable(orderTableModel);
		
		
		scrollpane = new JScrollPane(orderTable);   
		
		orderTable.setFont(FontUtil.menuFont2);
		orderTable.setShowGrid(false);
		 //设置选中行的背景色
		orderTable.setSelectionBackground(new Color(0xADD6DC));
		
	    orderTable.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent e) {
				//char a = (char)orderTable.getValueAt(orderTable.getSelectedRow(),orderTable.getSelectedColumn());
				//System.out.println(a);
			if(orderTable.getValueAt(orderTable.getSelectedRow(),orderTable.getSelectedColumn())!=null)
			{
				
				String a = (String)orderTable.getValueAt(orderTable.getSelectedRow(),orderTable.getSelectedColumn());
				int id =(Integer)orderTable.getValueAt(orderTable.getSelectedRow(),0);
				if(a=="+") {
					shoppingCar.add1(id);
					MainView.count+=1;
					Menu1View.countText.setText(String.valueOf(MainView.count));
					Menu2View.countText.setText(String.valueOf(MainView.count));
					Menu3View.countText.setText(String.valueOf(MainView.count));
					Menu4View.countText.setText(String.valueOf(MainView.count));
				}else if(a=="-") {
					shoppingCar.delete1(id);
					MainView.count-=1;
					Menu1View.countText.setText(String.valueOf(MainView.count));
					Menu2View.countText.setText(String.valueOf(MainView.count));
					Menu3View.countText.setText(String.valueOf(MainView.count));
					Menu4View.countText.setText(String.valueOf(MainView.count));
				}
				
			}
			
			repaint();
			}
			}); 
		
		
		Other1 other1 = new Other1();
		other1.fitTableColumns(orderTable);
	    
		
	    
	
		
		
		//表格文字居中显示
		DefaultTableCellRenderer r=new DefaultTableCellRenderer();
        r.setHorizontalAlignment(JLabel.CENTER);
        orderTable.setRowHeight(30);
        orderTable.setDefaultRenderer(Object.class,r);
        orderTable.setBackground(Color.white);

		
		bottomPanel = new JPanel();
		
		bottomPanel.setBackground(Color.white);
		clearButton = new JButton("全部清除");
	    clearButton.setBackground(new Color(0x59C593));
	 
	    bottomPanel.add(clearButton);
	    clearButton.addActionListener(new ActionListener(){
			 @Override
			 public void actionPerformed(ActionEvent e) {
			 	shoppingCar.clear();
			 	OrderView.this.dispose();
			 	new OrderView();
			 	MainView.count=0;
			 	Menu1View.countText.setText(String.valueOf(MainView.count));
				Menu2View.countText.setText(String.valueOf(MainView.count));
				Menu3View.countText.setText(String.valueOf(MainView.count));
				Menu4View.countText.setText(String.valueOf(MainView.count));
			 }
			 });
	    
	    
	    
	    
		bottomPanel.setFont(FontUtil.menuFont2);
		commitButton = new JButton("确认结算");
		commitButton.setBackground(new Color(0x59C593));
		commitButton.addMouseListener(new MouseListener() {
			@Override
			public void mouseClicked(MouseEvent e) {
			OrderAndInfService orderAndInfService = new OrderAndInfService();
				//addOrder(shoppingcar.TotalPrice);
			
			System.out.println(id+"第一次");
			id = orderAndInfService.addOrder(shoppingCar.totalPrice); //创建订单表
			
			System.out.println(id+"第二次");
			tp = shoppingCar.totalPrice;	 
		     
			Set keies = shoppingCar.container.keySet();
			for (Object key : keies) {
				Orderinf orderInf = new Orderinf();
		        orderInf.setOrderId(id);
		        orderInf.setName(shoppingCar.container.get(key).getProduct().getName());;
		        orderInf.setPrice(shoppingCar.container.get(key).getProduct().getPrice());
		        orderInf.setCount(shoppingCar.container.get(key).getCount());
		        orderInf.setItemPrice(shoppingCar.container.get(key).getItemPrice());
		        orderAndInfService.addOrderInf(orderInf);  //创建订单明细表
			}
			//Id,shoppingCar.totalPrice
			
			new sumView();
			shoppingCar.clear();
			 MainView.count=0;
			    Menu1View.countText.setText(String.valueOf(MainView.count));
				Menu2View.countText.setText(String.valueOf(MainView.count));
				Menu3View.countText.setText(String.valueOf(MainView.count));
				Menu4View.countText.setText(String.valueOf(MainView.count));
			OrderView.this.dispose();
	        
				
				
			}

			@Override
			public void mousePressed(MouseEvent e) {
				 
			}

			@Override
			public void mouseReleased(MouseEvent e) {
			
				
			}

			@Override
			public void mouseEntered(MouseEvent e) {
				commitButton.setForeground(Color.white);
				
			}

			@Override
			public void mouseExited(MouseEvent e) {
				commitButton.setForeground(Color.black);
				
			}
			
		});
		
	    
		bottomPanel.add(commitButton);
		textPanel=new JLabel();
		textPanel.setText("总计："+shoppingCar.totalPrice);
		textPanel.setFont(FontUtil.menuFont2);
		bottomPanel.add(textPanel);
		
		
		
		
		this.add(scrollpane,"Center");
		this.add(bottomPanel,"South");
		
		setVisible(true);
			
		
		
	}
	public static void main(String[] args) {
		try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        }catch(Exception e) {
        	System.out.println(e);
        }
		OrderView orderView = new OrderView();
		
		
	}

	
	

}
